package com.shuzhi.stationarysource.modules.job.service.impl;

import com.shuzhi.stationarysource.modules.job.domain.FixedSource;
import com.shuzhi.stationarysource.modules.job.repository.FixedSourceRepository;
import com.shuzhi.stationarysource.modules.job.service.FixedSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FixedSourceServiceImpl
 * @Description FixedSourceServiceImpl
 * @Author 孙峰
 * @Date 2020/3/10 11:03
 * @Version 1.0
 */
@Slf4j
@Service
@Transactional
public class FixedSourceServiceImpl implements FixedSourceService {
    @Autowired
    private FixedSourceRepository fixedSourceRepository;

    @Override
    public void savaAll(List<FixedSource> list) {
        int length = list.size();
        List<FixedSource> newlist = new ArrayList<FixedSource>();
        if (length == 0) {
            log.info("集合为空");
        } else {
            for (FixedSource fixedSource : list) {
                if (newlist.size() == 1000) {
                    fixedSourceRepository.saveAll(newlist);
                    fixedSourceRepository.flush();
                    newlist.clear();
                    log.info("批次插入数据库");
                }
                newlist.add(fixedSource);
            }
            fixedSourceRepository.saveAll(newlist);
        }
    }

    @Override
    public FixedSource save(FixedSource fixedSource) {
        return fixedSourceRepository.save(fixedSource);
    }
}
